import os, sys
import pytest
import time
import datetime
from pytest_jsonreport.plugin import JSONReport

base_dir = os.path.dirname(os.path.abspath(__file__))
print("zenmzu")
sys.path.insert(0, base_dir)
from Config.Config_file_Path import case_path, report_path
from Config.Send_info_Mail_Ding import dingding_content,email_content
from TongYong import Send_message


class RunCase:
    print("zheli")
    dingding_group = "qa"
    test_time = datetime.datetime.now()
    title_time = test_time.strftime("%Y%m%d %H%M%S")
    report_html = os.path.join(report_path, "report_%s.html"%(title_time))
    report_title = "Chen_project_test自动化接口测试报告"
    plugin = JSONReport()


    def get_run_arg(self):
        print("来到这里？")
        if len(sys.argv)>1:
            print("来这儿了吗?")
            tag = sys.argv[1].strip()
            if tag == "all":
                return []
            return ["-m",tag]
        print("这儿？")
        return []

    def run_case(self):
        print("还是这里？")
        tag = self.get_run_arg()

        print("这里吗？")
        command = ["-vs", case_path, "--pytest_report", self.report_html, "--pytest_title",
                     self.report_title, '--json-report-file=none']
        command.extend(tag)
        print("还有这里？")
        pytest.main(command, plugins=[self.plugin])
        print("最后？")

        summary = self.plugin.report.get("summary")
        self.passed = summary.get("passed", 0)
        self.failed = summary.get("failed", 0)
        self.skipped = summary.get("skipped", 0)
        self.total = summary.get("total", 0)

    # def send_report(self):
    #     # 钉钉
    #     content = dingding_content % (self.total, self.passed, self.failed, self.skipped)
    #     Send_message.DING.send_more_group(content,self.dingding_group)
    #
    #     # 邮件
    #     eamail_content = email_content % (self.total, self.passed, self.failed, self.skipped)
    #     subject = self.report_title + time.strftime("%Y-%m-%d %H:%M:%S")
    #     Send_message.send_mail(subject, eamail_content, self.report_html)

    @classmethod
    def main(cls):
        obj = cls()
        obj.run_case()
        # obj.send_report()


if __name__ == '__main__':
    RunCase.main()
